package com.itheima.tanhua.controller;

import com.itheima.tanhua.mongo.Movement;
import com.itheima.tanhua.service.CommentService;
import com.itheima.tanhua.service.MovementService;
import com.itheima.tanhua.service.TanHuaService;
import com.itheima.tanhua.vo.MovementsVo;
import com.itheima.tanhua.vo.PageResult;
import com.itheima.tanhua.vo.TodayBest;
import com.itheima.tanhua.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Lff
 * @date 2022/3/26
 * 动态相关功能接入
 */
@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;


    @Autowired
    private CommentService commentService;

    /**
     * 发布动态
     *
     * @param movement
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity publisher(Movement movement, MultipartFile[] imageContent) {
        movementService.publisher(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看自己或者指定好友的所有动态信息
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity getAllMovement(@RequestParam(required = false, defaultValue = "1") Integer page,
                                         @RequestParam(required = false, defaultValue = "10") Integer pagesize,
                                         @RequestParam(required = false) Long userId) {
        PageResult pr = movementService.getAllMovement(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 获取好友动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getFriendMovements(@RequestParam(required = false, defaultValue = "1") Integer page,
                                             @RequestParam(required = false, defaultValue = "10") Integer pagesize) {
        //调用Service来获取返回结果集
        PageResult results = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(results);
    }

    /**
     * 获取推荐动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity getRecommendMovements(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                @RequestParam(required = false, defaultValue = "10") Integer pagesize) {
        //调用Service来获取返回结果集
        PageResult results = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(results);
    }


    /**
     * 根据动态id查询指定id
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity getMovementById(@PathVariable("id") String movementId) {
        MovementsVo result = movementService.findMovementOne(movementId);
        return ResponseEntity.ok(result);
    }

    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity doLike(@PathVariable(value = "id", required = false) String movementId) {
        Integer count = commentService.doLike(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity doDisLike(@PathVariable(value = "id", required = false) String movementId) {
        Integer count = commentService.doDisLike(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity doLove(@PathVariable(value = "id", required = false) String movementId) {
        Integer count = commentService.doLove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 不喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity doUnLove(@PathVariable(value = "id", required = false) String movementId) {
        Integer count = commentService.doUnLove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 获取来访记录（访客记录）
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> lists = movementService.findVisitors();
        return ResponseEntity.ok(lists);
    }
}
